package com.hlkj.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBConnection {
	private static String username = "";
	private static String password = "";
	private static String url = "";
	private static String driver = "";
	static {
		try {
			Properties props = new Properties();
			props.load(DBConnection.class.getClassLoader().getResourceAsStream(
					"jdbc.properties"));

			url = props.getProperty("url");
			username = props.getProperty("username");
			password = props.getProperty("password");
			driver = props.getProperty("driverClassName");
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public  Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}

	public  void close(Connection conn, PreparedStatement ps)
			throws SQLException {
		if (ps != null) {
			ps.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	public  void close(Connection conn, PreparedStatement ps, ResultSet rs)
			throws SQLException {
		if (rs != null) {
			rs.close();
		}
		if (ps != null) {
			ps.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	public  void close(Connection conn, Statement st) throws SQLException {
		if (st != null) {
			st.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	public  void close(Connection conn, Statement st, ResultSet rs)
			throws SQLException {
		if (rs != null) {
			rs.close();
		}
		if (st != null) {
			st.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

}
